﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using CardInChina.Bussiness.Frontend.Interface;
using CardInChina.Bussiness.Frontend.Models;
using CardInChina.Common.Tool;
using CardInChina.Web.Mvc.Models;

namespace CardInChina.Web.Mvc.Controllers
{
    public class WidgetController : CICBasicController
    {
        private CardInfoBusinessService ss = new CardInfoBusinessService();
        private CardBusinessService cbs = new CardBusinessService();
        private GameBusinessService gbs = new GameBusinessService();
        private TopNewsService ts = new TopNewsService();
        private UserService us = new UserService();

        //
        // GET: /SearchBar/
        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult SearchBar()
        {
            ViewData.Model = ss.GetHotTopSearchItem(4);
            return PartialView();
        }

        public PartialViewResult UserInfo()
        {
            UserInfoShowInWidget uisw = new UserInfoShowInWidget();
            ViewData.Model = uisw;

            if (CurrentUser.IsLogon)
            {
                uisw.IsAuthenticated = true;
                uisw.UserName = CurrentUser.Details.Entity.GetDisplayName();
            }
            else
            {
                uisw.IsAuthenticated = false;
            }

            return PartialView();
        }

        [HttpPost]
        public PartialViewResult UserInfo(UserLoginInfo uli)
        {
            UserInfoShowInWidget uisw = new UserInfoShowInWidget();
            ViewData.Model = uisw;

            // a slight delay to simulate the real network traffic
            Thread.Sleep(500);

            if (uli.validCode == CurrentUser.HipCode)
            {
                var userInfo = us.ValidUserLogIn(uli);
                if (userInfo != null)
                {
                    CurrentUser.Details = userInfo;         
                    uisw.IsAuthenticated = true;
                    uisw.UserName = userInfo.Entity.GetDisplayName();
                }
                else
                {
                    uisw.IsAuthenticated = false;
                    uisw.Msg = "错误的用户名或密码，请重新输入！";
                }
            }
            else
            {
                uisw.IsAuthenticated = false;
                uisw.Msg = "验证码错误，请重新输入！";
            }

            return PartialView();
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult GlobalPurchasing()
        {
            return PartialView();
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult Sale()
        {
            ViewData.Model = cbs.Get2SalePromotionCard();
            return PartialView();
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult GameClass()
        {
            ViewBag.HotGames = gbs.GetHotGames();
            ViewBag.GamesByClass = gbs.GetGamesWithAllIndex();
            ViewBag.Others = gbs.GetOtherGames();
            return PartialView();
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult CustomerService()
        {
            return PartialView();
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult SiteBulletin()
        {
            ViewData.Model = ts.GetTop5News();
            return PartialView();
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult SaleRank()
        {
            ViewData.Model = ss.Get10SaleHotTopCardInfo();
            return PartialView();
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public PartialViewResult GameRank()
        {
            ViewData.Model = gbs.Get10HotTopGames();
            return PartialView();
        }

        public ActionResult GetValidateCode()
        {
            string code = CaptchaHelper.GetRandomNumString(4);
            CurrentUser.HipCode = code;
            return File(CaptchaHelper.CreateCaptchaImagePNG(code), @"image/png");
        }

        //accept all common ajax login
        [HttpAjaxPost]
        public ActionResult AjaxLogIn(UserLoginInfo uli)
        {
            var userInfo = us.ValidUserLogIn(uli);
            if (userInfo != null)
            {
                CurrentUser.Details = userInfo;
                return JavaScript(JavaScriptAfterLogInSuccess());
            }
            else
            {
                CurrentUser.Details = null;
                return View();
            }
        }

        private string JavaScriptAfterLogInSuccess()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type=\"text/javascript\">");
            sb.Append("$('#common_dialog_c').dialog(\"close\");");
            sb.Append("window.location.reload();");
            sb.Append("</script>");
            return sb.ToString();
        }

        //[OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        // 不能缓存，否则Telerik下拉框无效
        public PartialViewResult Top20Sales()
        {
            ViewBag.CardInfoesShow = ss.GetCardInfoesShowInHomePage();
            return PartialView();
        }


    }
}
